package com.woniu.adopt.result;

import com.alibaba.fastjson.JSONObject;
import com.woniu.adopt.ExceptionHandler.BaseErrorInfoInterface;
import com.woniu.adopt.ExceptionHandler.ExceptionEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultResponse<T> {
    private int code ;
    private String msg ;
    private T data ;
    private String token ; // 保存jwt 生成的token =================

    public ResultResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultResponse(BaseErrorInfoInterface errorInfo) {
        this.code = Integer.parseInt(errorInfo.getResultCode());
        this.msg = errorInfo.getResultMsg();
    }



    public void setMessage(String message) {
        this.msg = message;
    }


    

    /**
     * 成功
     *
     * @return
     */
    public static ResultResponse success() {
        return success(null);
    }

    /**
     * 成功
     * @param data
     * @return
     */
    public static ResultResponse success(Object data) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(Integer.parseInt(ExceptionEnum.SUCCESS.getResultCode()));
        rb.setMessage(ExceptionEnum.SUCCESS.getResultMsg());
        rb.setData(data);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultResponse error(BaseErrorInfoInterface errorInfo) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(Integer.parseInt(errorInfo.getResultCode()));
        rb.setMessage(errorInfo.getResultMsg());
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultResponse error(String code, String message) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(Integer.parseInt(code));
        rb.setMessage(message);
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultResponse error( String message) {
        ResultResponse rb = new ResultResponse();
        rb.setCode(-1);
        rb.setMessage(message);
        rb.setData(null);
        return rb;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

}
